/*
 * Copyright 2016-2018. Individual ownership panw
 *
 * Tech blog at http://blog.csdn.net/hatsune_miku_.
 *
 * Contact Mailbox at mikuismywifu@gmail.com.
 *
 * panwang All Rights Reserved.
 */
package com.zhixun.mobile.modules.jiujiudevice.device.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collection;

/**
 * File Name:BigDecimalUtils
 *
 * @author:panwang
 * @description: 浮点类型计算工具类
 * @date:2017/11/24
 * @version:V1.0
 * @see:jdk8 Copyright (c) 2017, mikuismywifu@gmail.com All Rights Reserved.
 */
public class BigDecimalUtils {

    private static final String DECIMAL_DEFAULT="0.00";

    private static final DecimalFormat DECIMALFORMAT_DEFAULT = new DecimalFormat("0.00");

    /**
     * 将数值转换为BigDecimal
     *
     * @param o object
     * @return
     */
    public static BigDecimal objectToBigDecimal (Object o) {
        return BigDecimal.valueOf(Double.valueOf(String.valueOf(o)));
    }

    /**
     * 将两个数值相加并返回浮点类型
     *
     * @param o1 object
     * @param o2 object
     * @return bigDecimal
     */
    public static BigDecimal plus (Object o1, Object o2) {
        return BigDecimal.valueOf(Double.valueOf(String.valueOf(o1)))
                .add(BigDecimal.valueOf(Double.valueOf(String.valueOf(o2))));
    }

    /**
     * 参数一减去参数二返回浮点类型
     *
     * @param o1 object
     * @param o2 object
     * @return
     */
    public static BigDecimal subtract (Object o1, Object o2) {
        return BigDecimal.valueOf(Double.valueOf(String.valueOf(o1)))
                .subtract(BigDecimal.valueOf(Double.valueOf(String.valueOf(o2))));
    }


    /**
     * 计算两个数值的乘积
     *
     * @param o1 object
     * @param o2 object
     * @return
     */
    public static BigDecimal multiply (Object o1, Object o2) {
        return BigDecimal.valueOf(Double.valueOf(String.valueOf(o1)))
                .multiply(BigDecimal.valueOf(Double.valueOf(String.valueOf(o2))));
    }


    /**
     * 两个数相除四舍五入
     * 根据指定保留小数，如果不指定则默认保留两位
     *
     * @param o1 object
     * @param o2 object
     * @return
     */
    public static BigDecimal divide(Object o1, Object o2,Integer i) {
        if (i == null) {
            i = 2;
        }
        return BigDecimal.valueOf(Double.valueOf(String.valueOf(o1)))
                .divide(BigDecimal.valueOf(
                        Double.valueOf(String.valueOf(o2))),i,BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 格式化浮点类型成指定的小数四舍五入,缺省格式0.00
     * 设置格式请看DecimalFormat
     *
     * @param o object
     * @param format string
     * @return strng
     * @see java.text.DecimalFormat
     */
    public static String objectFormatToString(Object o,String format) {
        if (format == null) {
            format = DECIMAL_DEFAULT;
        }
        DecimalFormat decimalFormat = new DecimalFormat(format);
        return decimalFormat.format(
                BigDecimal.valueOf(Double.valueOf(String.valueOf(o))));
    }

    /**
     * 格式化浮点类型成指定的小数四舍五入,缺省格式0.00
     * 设置格式请看DecimalFormat
     *
     * @param o object
     * @param format string
     * @return bigDecimal
     * @see java.text.DecimalFormat
     */
    public static BigDecimal objectFormatToBD(Object o,String format) {
        if (format == null) {
            format = DECIMAL_DEFAULT;
        }
        DecimalFormat decimalFormat = new DecimalFormat(format);
        return BigDecimalUtils.objectToBigDecimal(
                decimalFormat.format(
                BigDecimal.valueOf(Double.valueOf(String.valueOf(o)))));
    }
    
    public static String DoubleFormatToDo(Double d,String format){
    	if (format == null) {
            format = DECIMAL_DEFAULT;
        }
    	DecimalFormat decimalFormat = new DecimalFormat(format);
    	System.out.println(decimalFormat.format(d));
    	return decimalFormat.format(d);
    }
    

/**
     * String转换成double 保留N位小数。
     * @param a
     * @return
     */
    public static double stringToDouble(String a){
    	double b = Double.valueOf(a);
    	DecimalFormat df = new DecimalFormat("0.00");//此为保留1位小数，若想保留2位小数，则填写#.00  ，以此类推
    	String temp = df.format(b);
    	b = Double.valueOf(temp);
    	return b;
    }
    public static void main(String[] args) {
    	System.out.println(stringToDouble(DoubleFormatToDo(0d,"#0.00")));
	}
 }
